home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 June / MacFormat 25.iso / Shareware City / Developers / ICAppSourceKit1.0 / RequiredEventSupport.p < prev    next >
Encoding:
Text File  |  1994-08-20  |  4.0 KB  |  128 lines  |  [TEXT/PJMM]

  1. unit RequiredEventSupport;
  2.  
  3. interface
  4.  
  5.     uses
  6.         AppleEvents;
  7.  
  8.     function InitAppleEvents (DoOApp, DoODoc, DoPrint, DoQuit: Ptr): OSErr;
  9.     function GotRequiredParams (theAppleEvent: AppleEvent): OSErr;
  10.  
  11. implementation
  12.  
  13.     function DoOApp (p: ptr): OSErr;
  14.     inline
  15.         $205F, (* movea.l    (a7)+,a0        ; pop proc/func address    *)
  16.         $4E90; (* jsr            (a0)                ; call proc/func                *)
  17.  
  18.     function DoDocs (fs: FSSpec; p: ptr): OSErr;
  19.     inline
  20.         $205F, (* movea.l    (a7)+,a0        ; pop proc/func address    *)
  21.         $4E90; (* jsr            (a0)                ; call proc/func                *)
  22.  
  23.     function DoQuit (p: ptr): OSErr;
  24.     inline
  25.         $205F, (* movea.l    (a7)+,a0        ; pop proc/func address    *)
  26.         $4E90; (* jsr            (a0)                ; call proc/func                *)
  27.  
  28.     function GotRequiredParams (theAppleEvent: AppleEvent): OSErr;
  29.         var
  30.             typeCode: DescType;
  31.             actualSize: Size;
  32.             err: OSErr;
  33.     begin
  34.         err := AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard, typeCode, nil, 0, actualSize);
  35. (* nil ok: need only function result *)
  36.  
  37.         if err = errAEDescNotFound then        (* we got all the required params: all is ok *)
  38.             GotRequiredParams := noErr
  39.         else if err = noErr then
  40.             GotRequiredParams := errAEEventNotHandled
  41.         else
  42.             GotRequiredParams := err;
  43.     end; (* GotRequiredParams *)
  44.  
  45.     function HandleOAPP (theAppleEvent, reply: AppleEvent; openappp: ptr): OSErr;
  46. (* <aevt> }
  47. {    kCoreEventClass kAEOpenApplication}
  48. {*)
  49.         var
  50.             oe: OSErr;
  51.     begin
  52.     (* We don't expect any params at all, but check in case the client requires any *)
  53.         oe := GotRequiredParams(theAppleEvent);
  54.         oe := DoOApp(openappp);
  55.         HandleOAPP := oe;
  56.     end;
  57.  
  58.     function HandleDocs (theAppleEvent, reply: AppleEvent; dodocp: ptr): OSErr;
  59. (* <aevt>}
  60. {    kCoreEventClass kAEOpenDocuments}
  61. {    kCoreEventClass kAEPrintDocuments}
  62. {*)
  63.         var
  64.             myFSS: FSSpec;
  65.             docList: AEDescList;
  66.             index, itemsInList: longint;
  67.             actualSize: Size;
  68.             keywd: AEKeyword;
  69.             typeCode: descType;
  70.             ignoreWPtr: WindowPtr;
  71.             oe, ooe: OSErr;
  72.     begin
  73.         oe := AEGetParamDesc(theAppleEvent, keyDirectObject, typeAEList, docList);
  74.         if oe = noErr then begin
  75.             ooe := GotRequiredParams(theAppleEvent);
  76. (* now get each alias from the list (as an FSSSpec) and open the associated file. *)
  77.             oe := AECountItems(docList, itemsInList);
  78.             for index := 1 to itemsInList do begin
  79.                 ooe := AEGetNthPtr(docList, index, typeFSS, keywd, typeCode, @myFSS, sizeof(myFSS), actualSize);
  80. (* coercion does alias->fsspec *)
  81.                 if ooe = noErr then
  82.                     ooe := DoDocs(myFSS, dodocp);
  83.             end;
  84.             ooe := AEDisposeDesc(docList);
  85.         end;
  86.         HandleDocs := oe;
  87.     end; (* HandleDocs *)
  88.  
  89.     function HandleQUIT (theAppleEvent, reply: AppleEvent; quitp: ptr): OSErr;
  90. (* <aevt> }
  91. {    kCoreEventClass kAEQuitApplication}
  92. {*)
  93.         var
  94.             oe: OSErr;
  95.             errStr: Str255;
  96.             willQuit: Boolean;                (* did the user allow the quit or cancel *)
  97.     begin
  98. (* We don't expect any params at all, but check in case the client requires any *)
  99.         oe := GotRequiredParams(theAppleEvent);
  100.         oe := DoQuit(quitp);                (* set global boolean: app will exit at end of event loop *)
  101.         if reply.dataHandle <> nil then        (* a reply is sought *)
  102.             begin
  103.             if oe = noErr then
  104.                 errStr := 'OK'
  105.             else
  106.                 errStr := 'user cancelled quit';
  107.             oe := AEPutParamPtr(reply, keyErrorString, typeChar, Ptr(@errStr[1]), length(errStr));
  108.         end;
  109.         HandleQUIT := oe;
  110.     end;
  111.  
  112.     function InitAppleEvents (DoOApp, DoODoc, DoPrint, DoQuit: Ptr): OSErr;
  113.         var
  114.             aevtErr: OSErr;
  115.     begin
  116.         aevtErr := noErr;
  117.         if (aevtErr = noErr) and (DoOApp <> nil) then
  118.             aevtErr := AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, @HandleOAPP, longint(DoOApp), false);
  119.         if (aevtErr = noErr) and (DoODoc <> nil) then
  120.             aevtErr := AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, @HandleDocs, longint(DoODoc), false);
  121.         if (aevtErr = noErr) and (DoPrint <> nil) then
  122.             aevtErr := AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, @HandleDocs, longint(DoPrint), false);
  123.         if (aevtErr = noErr) and (DoQuit <> nil) then
  124.             aevtErr := AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, @HandleQUIT, longint(DoQuit), false);
  125.         InitAppleEvents := aevtErr;
  126.     end;
  127.  
  128. end. (* RequiredEventSupport *)